package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.HomeRecommendSubjectApi;
import com.yoda.mall.model.entity.sms.HomeRecommendProduct;
import com.yoda.mall.model.entity.sms.HomeRecommendSubject;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.HomeRecommendSubjectMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 17日 11:12
 */
@DubboService
public class HomeRecommendSubjectApiImpl implements HomeRecommendSubjectApi {
    @Resource
    private HomeRecommendSubjectMapper homeRecommendSubjectMapper;

    @Override
    public Integer createRecommendProduct(List<HomeRecommendSubject> homeRecommendSubjects) {
        return homeRecommendSubjectMapper.saveBatch(homeRecommendSubjects);
    }

    @Override
    public void deleteRecommendSubjectByIds(List<Long> ids) {
        homeRecommendSubjectMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateRecommendSubjectStatus(List<Long> ids, Integer recommendStatus) {
        homeRecommendSubjectMapper.updateRecommendSubjectStatus(ids,recommendStatus);
    }

    @Override
    public void changeRecommendSubjectSort(Long id, Integer sort) {
        HomeRecommendSubject subject = new HomeRecommendSubject();
        subject.setId(id);
        subject.setSort(sort);
        homeRecommendSubjectMapper.updateById(subject);
    }

    @Override
    public PageResult<HomeRecommendSubject> getRecommendSubjectPage(Integer page, Integer pageSize, String subjectName, Integer recommendStatus) {
        Page<HomeRecommendSubject> productPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<HomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(subjectName), HomeRecommendSubject::getSubjectName,subjectName);
        wrapper.eq(Objects.nonNull(recommendStatus), HomeRecommendSubject::getRecommendStatus,recommendStatus);
        wrapper.orderByDesc(HomeRecommendSubject::getSort);
        homeRecommendSubjectMapper.selectPage(productPage, wrapper);
        return new PageResult<>(page, pageSize, (int) productPage.getTotal(), productPage.getRecords());
    }
}
